package com.ljxy.score.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljxy.score.common.Result;
import com.ljxy.score.entity.domain.*;
import com.ljxy.score.service.myscoreService;
import com.ljxy.score.mapper.myscoreMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 86189
 * @description 针对表【myscore】的数据库操作Service实现
 * @createDate 2022-12-19 16:56:01
 */
@Service
public class myscoreServiceImpl extends ServiceImpl<myscoreMapper, myscore>
        implements myscoreService {
    @Resource
    private myscoreMapper myscoreMapper;

    @Resource
    private com.ljxy.score.service.mystudentService mystudentService;

    @Override
    public Result<?> getByClassId(Integer id) {
        if (id == 0) {
            List<myscore> myscores = myscoreMapper.getAllClass();
            return Result.success(myscores);
        }
        List<myscore> list = myscoreMapper.getByClassId(id);
        return Result.success(list);
    }

    @Override
    public Result<?> enterInformation(InformationOfExam information) {
        //1.判空
        myscore myscore = new myscore();
        myscore.setChina(information.getChina());
        myscore.setMath(information.getMath());
        myscore.setEng(information.getEng());
        String stuId = information.getStuId();
        myscore.setClassId(information.getClassId());
        myscore.setStuId(stuId);
        boolean isSuccess = this.save(myscore);
        if (!isSuccess) {
            return Result.error();
        }
        return Result.success(true);
    }

    @Override
    public Result<?> updateScore(updateDTO updateDTO) {
        myscore myscore = new myscore();
        myscore.setStuId(updateDTO.getStudentId());
        myscore.setChina(updateDTO.getChina());
        myscore.setMath(updateDTO.getMath());
        myscore.setEng(updateDTO.getEng());
        QueryWrapper<com.ljxy.score.entity.domain.myscore> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_id", updateDTO.getStudentId());
        boolean isSuccess = this.update(myscore, queryWrapper);
        return Result.success(isSuccess);
    }

    @Override
    public Result<?> addScore(addDTO addDTO) {
        myscore myscore = new myscore();
        myscore.setStuId(addDTO.getStudentId());
        myscore.setChina(addDTO.getChina());
        myscore.setClassId(addDTO.getClassId());
        myscore.setMath(addDTO.getMath());
        myscore.setEng(addDTO.getEng());
        mystudent mystudet = new mystudent();
        mystudet.setStuName(addDTO.getStudentName());
        mystudet.setStuId(addDTO.getStudentId());
        mystudet.setClazzId(addDTO.getClassId());
        boolean save = mystudentService.save(mystudet);
        boolean isSuccess = this.save(myscore);
        return Result.success(isSuccess && save);
    }

    @Override
    public Result<?> deleteScore(deleteDTO studentId) {
        QueryWrapper<myscore> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_id", studentId.getStudentId());
        boolean isSuccess = this.remove(queryWrapper);
        return Result.success(isSuccess);
    }
}




